package com.itheima.rocketmq.rocketmqdemo.base;

/**
 * @description:双重检查锁
 * @author: ycc
 * @time: 2021/3/30
 */
public class Sington {
    private Sington() {
    }

    private static volatile Sington instance;

    public static Sington getInstance() {
        if (instance == null) {
            synchronized (Sington.class) {
                if (instance == null) {
                    instance = new Sington();
                }
            }
        }
        return instance;
    }

    //CAS 比较交互
    int cas(long addr, long oldValue, long newValue) {
        if (addr != oldValue) {
            return 0;
        }
        addr = newValue;
        return 1;
    }
}


